<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 广告函数库
 * 
 * @desc 
 * @version 1.0.0 
 * @author  Jason
 * @date	2017年6月8日 下午4:50:06
 */
class Lib_advertisement extends Lib_database
{
	/** @var Mod_advertisement */
	private $mod_advertisement = null;
	
	// MARK 可以把 use trait; 语句放在比较显眼的部分：【Trait 所起的作用行为】与【在 class 中的引入位置】没啥关系！
	// 只要放在类体内&方法外就行！手册中的 Example 和 note 提及的代码很少不放在类首的！
	use libraries\common\DatabaseCURD;
	
	use libraries\common\ConditionParser;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->CI->config->load('enumeration');
		
		$this->CI->load->model('mod_advertisement');
		$this->mod_advertisement =& $this->CI->mod_advertisement;
		
		$this->set_render_config();
	}
	
	private function set_render_config()
	{
		$this->CI->load->model('mod_member');
		$foreign_keys = [
				'master' => [
						['position_id', ['name', 'width', 'height'], $this->mod_advertisement, 'position', 'id'],
						['member_id', ['name', 'email', 'mobile'], $this->CI->mod_member, 'master', 'id'],
						['member_id', ['nickname'], $this->CI->mod_member, 'info', 'id'],
				],
				'position' => [
						['member_id', ['name', 'email', 'mobile'], $this->CI->mod_member, 'master', 'id'],
						['member_id', ['nickname'], $this->CI->mod_member, 'info', 'id'],
				],
		];
		$time_fields = [
				'master' => [
						['begin_time', 'Y-m-d H:i:s'],
						['end_time', 'Y-m-d H:i:s'],
						['create_time', 'Y-m-d H:i:s'],
				],
				'position' => [
						['create_time', 'Y-m-d H:i:s'],
				],
		];
		$encode_fields = [
				'master' => [],
				'position' => [],
		];
		$this->foreign_keys = $foreign_keys;
		$this->time_fields = $time_fields;
		$this->encode_fields = $encode_fields;
	}
	
	public function get_ad_list_by_position_code($position_code)
	{
		# comes form lib_section
		#$advertisement = $this->CI->mod_advertisement;
		#$advertisement instanceof Mod_advertisement;
		
		$this->CI->load->config('dbconst', !true);
		$position_id = $this->CI->config->item($position_code, 'advertisement');
		$result = $this->mod_advertisement->get_ads_by_position($position_id);
		$result = $this->render_list($result, 'master', false, [
				[$this, 'parse_ad_item'],
		]);
		
		return $result;
	}
	
	public function get_ad_for_detail($id)
	{
		$item = $this->mod_advertisement->select_instance_by_id($id);
		$item = $this->render_list($item, 'master', true, [
				[$this, 'parse_ad_item'],
		]);
		return $item;
	}
	
	public function get_ads_for_list($conditions, $page = 1, $number = 12)
	{
		$data = $this->mod_advertisement->get_list($conditions, $page, $number);
		$data['data'] = $this->render_list($data['data'], 'master', false, [
				[$this, 'parse_ad_item'],
		]);
		return $data;
	}
	
	public function get_position_for_detail($id)
	{
		$item = $this->mod_advertisement->select_instance_by_id($id, 'position');
		$item = $this->render_list($item, 'position', true, [
				[$this, 'parse_position_item'],
		]);
		return $item;
	}
	
	public function get_positions_for_list($conditions, $page = 1, $number = 12)
	{
		$data = $this->mod_advertisement->get_list($conditions, $page, $number, 'position');
		$data['data'] = $this->render_list($data['data'], 'position', false, [
				[$this, 'parse_position_item'],
		]);
		return $data;
	}
	
	public function parse_ad_item($item)
	{
		$item['real_url'] = empty($item['url']) ? $this->get_resource_url($item) : $item['url'];
		$item['status_tags'] = $this->mod_advertisement->calculate_status_tags($item['status']);
		return $item;
	}
	
	public function parse_position_item($item)
	{
		$mapper_metrix = $this->CI->config->item('mapper_metrix')['advertisement']['master'];
		$item['type_text'] = @$mapper_metrix['type'][$item['type']];
		return $item;
	}
	
	/**
	 * 指定ID的广告位是否已经存在
	 * 
	 * @desc	MARK validation method: fake is_not_unique
	 * 			MARK 这里通过修改 CI-Libraries-Form_validation ... 这里不需要！！！
	 * @desc	适用场景：在添加广告时指定广告位ID，这个广告位是需要存在的！
	 * @date	2017年6月13日 下午5:42:48
	 *
	 * @param number $position_id
	 *
	 * @return boolean
	 */
	public function is_not_unique_fake_to_has_position_exists($position_id)
	{
		$selected_id = $this->mod_advertisement->select_fields_by_id($position_id, ['id'], 'position');
		return !empty($selected_id);
	}
	
	/**
	 * 是否是支持的类型
	 *
	 * @date	2017年6月13日 下午7:41:03
	 *
	 * @param string $type
	 *
	 * @return boolean
	 */
	public function is_supported_type($type)
	{
		$mapper_metrix = $this->CI->config->item('mapper_metrix')['advertisement']['master'];
		$type_mapper = $mapper_metrix['type'];
		
		return array_key_exists($type, $type_mapper);
	}
	
	private function get_resource_url($row)
	{
		$this->CI->load->helper('url_helper');
		return site_url([$row['resource_type'], 'detail' , $row['resource_id']]);
	}
	
	
	/**
	 * 广告显示不显示
	 *
	 * @author  Jason
	 * @date	2017年6月14日 下午2:31:25
	 *
	 * @param number $id
	 *
	 * @return [<status>, <content>]	一个两元素数组，第一个元素表示状态，第二个表示相应内容
	 * 	        eg:	[true, ['nowTag' => 'enabled']]
	 * 				[true, ['nowTag' => 'disabled']]
	 * 				[false, ['error']]
	 */
	public function toggle_status_enabled($id)
	{
		$status = true;
		$ultimateTag = $this->mod_advertisement->toggle_status_by_tag($id);
		
		return [$status, ['nowTag' => $ultimateTag]];
	}
}